Page({
  data: {
    houseList: [],
    dialogVisible: false,
    isEmpty: false
  },
  onShow() {
    this.getHouseList()
  },
  // 获取用户房屋
  async getHouseList() {
    const {
      code,
      data: houseList
    } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({
      houseList,
      isEmpty: houseList.length === 0
    })
  },
  swipeClose(ev) {
    const {
      position,
      instance
    } = ev.detail
    // 记录一下房屋id
    this.cellId = ev.mark.id
    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  // 删除房屋
  async deleteHouse() {
    // 检测有没有cellId
    if (!this.cellId) return wx.utils.toast('参数错误')
    // 调用删除接口
    const {
      code
    } = await wx.http.delete('/room/' + this.cellId)
    if (code !== 10000) return wx.utils.toast()
    // 1. 重新调用接口获取数据
    // 2. 利用filter   splice 从数组里把调接口删除的那一项删掉 
    const houseList = this.data.houseList.filter(item => item.id !== this.cellId)
    // 渲染数据
    this.setData({
      houseList,
      isEmpty: houseList.length === 0
    })
  },
  // dialong关闭的事件
  dialogClose(e) {
    // 点击确认e.detail 值伟confirm
    console.log(e.detail);
    if (e.detail === 'confirm') this.deleteHouse()
  },
  goDetail(e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})